﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;
using System.Collections;

namespace CIIS
{
    public partial class RoleUser : BaseForm
    {
        private RoleUser()
        {
            InitializeComponent();
        }
        t_sys_role role
        {
            get;
            set;
        }
        public RoleUser(t_sys_role _role)
        {
            InitializeComponent();
            LoadTree();
            InitUserGrid(_role.Id);
            InitRoleRight(_role.Id);
            role = _role;
            DevControlUtils.Instance.InitGridView(gridView1, false);
            DevControlUtils.Instance.InitGridView(gridView2, false);
        }
        private void InitRoleRight(string roleId)
        {
            string[] displays = Enum.GetNames(typeof(EMenus));
            Array values = Enum.GetValues(typeof(EMenus));
            List<t_sys_role_right> dataList = new List<t_sys_role_right>();
            Framework.QueryInfo info = new Framework.QueryInfo();
            info.QueryObject = "t_sys_role_right";
            info.AddParam("ROLE_ID", roleId);
            IList roleList = dao.Dao.FindList(info);
            for (int i = 0; i < displays.Length; i++)
            {
                t_sys_role_right r = new t_sys_role_right()
                {
                     RIGHT_ID =System.Convert.ToInt32(values.GetValue(i)).ToString(),
                    RIGHT_URL = displays[i]
                };
                for (var j = 0; j < roleList.Count; j++)
                {
                    var o = roleList[j] as t_sys_role_right;
                    if (o.RIGHT_ID.Equals(r.RIGHT_ID))
                    {
                        r.IsSelected = true;
                        roleList.RemoveAt(j);
                        break;
                    }
                  
                }
                dataList.Add(r);
            }
            gridControl2.DataSource = dataList;
        }
        private void InitUserGrid(string roleId)
        {
            IList userList = dao.GetListFromCache<t_sys_user>();
            IList<t_sys_user> list = new List<t_sys_user>();
            for (var i = 0; i < userList.Count; i++)
            {
                var user = userList[i] as t_sys_user;
                if (user.ROLE_ID!=null&&user.ROLE_ID.Equals(roleId))
                {
                    user.IsSelected = true;
                }
                list.Add(user);

            }
            gridControl1.DataSource = list;
            gridControl1.RefreshDataSource();
        }
        private void LoadTree()
        {
            var tmplist = dao.GetListFromCache<Contract.Domain.Office>();
            IList<Contract.Domain.Office> list = new List<Contract.Domain.Office>();
            IList<Contract.Domain.Office> treeList = new List<Contract.Domain.Office>();
            for (var i = 0; i < tmplist.Count; i++)
            {
                var o = tmplist[i] as Office;
                list.Add(o);
            }
            var tmp = list.Where(m => string.IsNullOrEmpty(m.ParentId)).OrderBy(m => m.Seq).ToList();
            if (tmp.Count > 0)
            {
                foreach (var o in tmp)
                {
                    o.Name = "-" + o.Name;
                    treeList.Add(o);
                    loadChild(list, treeList, o, 1);
                }
            }
            repositoryItemLookUpEdit1.DataSource = null;
            this.repositoryItemLookUpEdit1.DataSource = treeList;
            repositoryItemLookUpEdit1.DisplayMember = "Name";
            repositoryItemLookUpEdit1.ValueMember = "Id";
        }
        private void loadChild(IList<Contract.Domain.Office> all, IList<Contract.Domain.Office> tree, Office current, int depth)
        {
            string s = "|-";
            for (var i = 0; i < depth; i++)
            {
                s += "-";
            }
            var tmp = all.Where(m => !string.IsNullOrEmpty(m.ParentId) && m.ParentId.Equals(current.Id)).OrderBy(m => m.Seq).ToList();
            if (tmp.Count > 0)
            {
                foreach (var o in tmp)
                {
                    o.Name = s + o.Name;
                    tree.Add(o);
                    loadChild(all, tree, o, depth + 1);
                }
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                IList<t_sys_role_right> list = this.gridView2.DataSource as IList<t_sys_role_right>;
                var roleList = list.Where(m => m.IsSelected).ToList();
                System.Collections.ArrayList ulist = new ArrayList();
                if (roleList != null && roleList.Count > 0)
                {
                    foreach (var o in roleList)
                    {
                        t_sys_role_right r = new t_sys_role_right();
                        r.RIGHT_ID = o.RIGHT_ID;
                        r.RIGHT_URL = o.RIGHT_URL;
                        r.ROLE_ID = role.Id;
                        r.State.MarkNew();
                        ulist.Add(r);
                    }
                }
                IList<t_sys_user> tmpuserList = (this.gridView1.DataSource as IList<t_sys_user>).Where(m => m.IsSelected).ToList();
                System.Collections.ArrayList userList = new ArrayList();
                foreach (var o in tmpuserList)
                {
                    userList.Add(o.Id);
                }
                #region  删除原有记录
                Framework.QueryInfo info = new Framework.QueryInfo();
                info.CustomSQL = "update t_sys_user set ROLE_ID=null where ROLE_ID=:ROLE_ID";
                info.Parameters.Add("ROLE_ID", role.Id);
                dao.Dao.ExecuteNonQuery(info);
                info = new Framework.QueryInfo();
                info.CustomSQL = "delete from t_sys_role_right where ROLE_ID=:ROLE_ID";
                info.Parameters.Add("ROLE_ID", role.Id);
                dao.Dao.ExecuteNonQuery(info);
                #endregion
                #region 设置新权限
                info = new Framework.QueryInfo();
                info.CustomSQL = "update t_sys_user set ROLE_ID=:ROLE_ID";
                info.Parameters.Add("ROLE_ID", role.Id);
                info.AddParam("Id", userList.ToArray(typeof(string)));
                dao.Dao.ExecuteUpdate(info);
                if (ulist.Count > 0)
                {
                    dao.Dao.SaveOrUpdateAll(ulist);
                }
                #endregion
                MessageBox.Show("设置成功");
            }
            catch (Exception ex)
            {
                Utility.JSUtil.log(ex.Message);
                MessageBox.Show("设置失败，因为："+ex.Message);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
